package middle;

import java.util.PriorityQueue;
import java.util.Queue;

public class Solution_215 {

    public int findKthLargest(int[] nums, int k) {
        Queue<Integer> queue = new PriorityQueue<>((e1, e2) -> e1 - e2);
        for (int num : nums) {
            queue.add(num);
            if (queue.size() > k) {
                queue.poll();
            }
        }
        return queue.poll();
    }

    public static void main(String[] args) {
        Solution_215 model = new Solution_215();
        System.out.println(model.findKthLargest(new int[]{3, 5}, 1));
        System.out.println(model.findKthLargest(new int[]{3, 2, 1, 5, 6, 4}, 2));
        System.out.println(model.findKthLargest(new int[]{3, 2, 3, 1, 2, 4, 5, 5, 6}, 4));
    }
}
